<?php

/**
 * @author ict.sonla.gov.vn
 * @copyright 2012
 */

defined('DSVH') or die('Restricted access');

//G?i t?p tin giao di?n trong thu m?c view/theme/template tuong ?ng
 if(!$_SESSION['loged']){
         header('LOCATION: index.php?module=canbo&act=login');  
         exit();
     }
include('controller/c_thongke.php');     
include ('model/m_tinhoatdong.php');
$page = $_GET['act'];
 switch($page){
    case 'list':
    //Get language
    require('language/tinhoatdong/list_tinhoatdong.php');
    $heading_title = heading_title;
    $button_add = button_add;
    $button_edit = button_edit;
    $button_delete = button_delete;
    $list_cattin = getListCattin(0,100);
    $ma_tin_hoat_dong = ma_tin_hoat_dong;
    $ten_tin_hoat_dong = ten_tin_hoat_dong;
    $thao_tac = thao_tac;
    $link_add ='index.php?module=tinhoatdong&act=add';
    if(isset($_POST['gui'])){
    if(empty($_POST['selected'])){
        $err['exist'] = chua_chon;
        $error_warning = $err['exist'];
        }
    }    
    if(!$err){
        $valid = true;    
    }
    else{
        $valid = false;
    }
    
    //kiem tra du lieu cua from
    if(($_SERVER['REQUEST_METHOD']=='POST')&&$valid){
      foreach($_POST['selected'] as $id){
             $tinhoatdong = gettinhoatdong($id);
             if($tinhoatdong['file']){
                $file = "..".$tinhoatdong['file'];
                if(file_exists($file)){
                 unlink($file);
                }
             }
             deltinhoatdong($id);
             $_SESSION['success'] = xoa_thanh_cong;
       }  
    header('LOCATION: '.$link_list);
    }
    
    //tim kiem
     if(trim($_POST['ten_tin_hoat_dong'])==""){
         $err1['exist'] = chua_nhap_ten;
         $error1_name = $err1['exist'];
     }
   
     if(!$err1){
            $valid1 = true;
     }else{
            $valid1 = false;
     }
     
     if($_REQUEST['page']==""){ $page = 1;}
     else{$page = $_REQUEST['page'];}
     //get input for class page
     $limit = 15;
     $start = ($page - 1) * $limit; 
     
     if(($_SERVER['REQUEST_METHOD']=='POST')&&$valid1){
        
        $list_tinhoatdong = getlisttinhoatdongvoithamso($_POST,$start,$limit);
        $total = total2($_POST);
     }else{
   
        $list_tinhoatdong = getListtinhoatdong($start, $limit);
        $total = total();
  
    }
    $paging = new Pagination();
    $paging->total = $total;
    $paging->limit = $limit;
    $paging->url = "index.php?module=tinhoatdong&act=list&page={page}";
    $paging->page = $page;
    $pagination = $paging->render();
     
    require('view/template/tinhoatdong/list_tinhoatdong.tpl');
    break;
	
	//case them chuc danh
	case 'add':
	require('language/tinhoatdong/add_tinhoatdong.php');
	$heading_title = heading_title;
    $ma_tin_hoat_dong = ma_tin_hoat_dong;
	$ten_tin_hoat_dong = ten_tin_hoat_dong;
    $motachitiet = mo_ta_chi_tiet;
    $motachung = mo_ta_chung;
    $hinhdaidien = hinh_dai_dien;
    $list_cattin = getListCattin(0,100);
    $link_list = 'index.php?module=tinhoatdong&act=list';
    
    if($_POST['ten_tin_hoat_dong']==""){
        $err['exist'] = chua_nhap_ten;
        $error_name = $err['exist'];
             }
    if(!$err){
        $valid = true;    
    }
    else{
        $valid = false;
    }
    if(($_SERVER['REQUEST_METHOD']=='POST')&&$valid)
    {
      
    inserttinhoatdong($_POST);
    $_SESSION['success'] = edit_success;
    //chuyen huong ve list
    header('LOCATION: '.$link_list);    
    }
    require('view/template/tinhoatdong/edit_tinhoatdong.tpl');
    break;
    
    //case sua dan toc
    case 'edit':
    require('language/tinhoatdong/edit_tinhoatdong.php');
    $heading_title = heading_title;
    $ma_tin_hoat_dong = ma_tin_hoat_dong;
    $ten_tin_hoat_dong = ten_tin_hoat_dong;
    $motachitiet = mo_ta_chi_tiet;
    $motachung = mo_ta_chung;
    $hinhdaidien = hinh_dai_dien;
    $list_cattin = getListCattin(0,100);
    $link_list = 'index.php?module=tinhoatdong&act=list';
    $id = $_REQUEST['id'];
    $tinhoatdong = gettinhoatdong($id);
    //$_POST['file'] = $tinhoatdong['file'];
    
    if($_SERVER['REQUEST_METHOD']=='POST')
    {
      
    edittinhoatdong($id, $_POST);
    $_SESSION['success'] = edit_success;
    //chuyen huong ve list
    header('LOCATION: '.$link_list);    
    }
        
    
    require('view/template/tinhoatdong/edit_tinhoatdong.tpl');
    break; 
    
    case 'del':
    get_file('language/tinhoatdong/del_tinhoatdong.php');
    $link_list = 'index?module=tinhoatdong&act=list';
    $id = $_REQUEST['id'];
    $tinhoatdong = gettinhoatdong($id);
    if($tinhoatdong['avatar']){
           unlink('./data/image/tinhoatdong/'.$tinhoatdong['avatar']);
         }
    deltinhoatdong($id);
     $_SESSION['success'] = del_success;
    //chuyen huong ve list
     header('LOCATION: '.$link_list);  
     exit();
         
    
    break;  
    
    
    case 'cat':
    //Get language
    require('language/tinhoatdong/list_cattin.php');
    $heading_title = heading_title;
    $button_add = button_add;
    $button_edit = button_edit;
    $button_delete = button_delete;
    
    $ma_tin_hoat_dong = ma_tin_hoat_dong;
    $ten_tin_hoat_dong = ten_tin_hoat_dong;
    $thao_tac = thao_tac;
    $link_add ='index.php?module=tinhoatdong&act=addcat';
    
    if($_POST['selected']==""){
        $err['exist'] = chua_chon;
        $error_name = $err['exist'];
        $error_warning = $err['exist'];
        }
    if(!$err){
        $valid = true;    
    }
    else{
        $valid = false;
    }
    
    if($_REQUEST['page']==""){ $page = 1;}
    else{$page = $_REQUEST['page'];}
    
    //get input for class page
      $limit = 5;
      $start = ($page - 1) * $limit; 
      
    //kiem tra du lieu cua from
    if(($_SERVER['REQUEST_METHOD']=='POST')&&$valid){
      foreach($_POST['selected'] as $id){
         delcattin($id);
       }  
   
    //chuyen huong ve list
    header('LOCATION: '.$link_list);
    }
    
    
     $list_cattin = getListCattin($start, $limit);
     $total = totalcattin();
  
    $paging = new Pagination();
    $paging->total = $total;
    $paging->limit = $limit;
    $paging->url = "index.php?module=tinhoatdong&act=cat&page={page}";
    $paging->page = $page;
    $pagination = $paging->render();
     
    require('view/template/tinhoatdong/list_cattin.tpl');
    break;
    
   	case 'addcat':
	require('language/tinhoatdong/add_cattin.php');
	$heading_title = heading_title;
    $ten_cat_tin = ten_danh_muc;
    $sap_xep = sap_xep;
    
    $list_cattin = getListCattin(0,100);
    $link_list = 'index.php?module=tinhoatdong&act=cat';
    
    if($_POST['ten_cat_tin']==""){
        $err['exist'] = chua_nhap_ten;
        $error_name = $err['exist'];
             }
    if(!$err){
        $valid = true;    
    }
    else{
        $valid = false;
    }
    if(($_SERVER['REQUEST_METHOD']=='POST')&&$valid)
    {
      
    insertcattin($_POST);
    $_SESSION['success'] = edit_success;
    //chuyen huong ve list
    header('LOCATION: '.$link_list);    
    }
    require('view/template/tinhoatdong/edit_cattin.tpl');
    break;
    
    //case sua dan toc
    case 'editcat':
    require('language/tinhoatdong/edit_cattin.php');
    $heading_title = heading_title;
    $ten_cat_tin = ten_danh_muc;
    $sap_xep = sap_xep;
    $list_cattin = getListCattin(0,100);
    $link_list = 'index.php?module=tinhoatdong&act=cat';
    $id = $_REQUEST['id'];
    $cattin = getcattin($id);
    //$_POST['file'] = $tinhoatdong['file'];
    
    if($_SERVER['REQUEST_METHOD']=='POST')
    {
      
    editcattin($id, $_POST);
    $_SESSION['success'] = edit_success;
    //chuyen huong ve list
    header('LOCATION: '.$link_list);    
    }
        
    
    require('view/template/tinhoatdong/edit_cattin.tpl');
    break; 
}
?>